package com.skywolf.chem.usercenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.common.sysconfig.utils.TLBeanUtils;
import com.skywolf.chem.usercenter.entity.User;
import com.skywolf.chem.usercenter.pojo.param.PasswordChangeParam;
import com.skywolf.chem.usercenter.pojo.param.UserCreateParam;
import com.skywolf.chem.usercenter.pojo.param.UserQueryParam;
import com.skywolf.chem.usercenter.pojo.param.UserUpdateParam;
import com.skywolf.chem.usercenter.pojo.vo.UserVO;
import com.skywolf.chem.usercenter.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController.java
 * @Author Leo
 * @Version 1.0.0
 * @Description 用户controller
 * @CreateTime 2020年05月12日 22:29:00
 */
@RestController
@RequestMapping("/usercenter/user")
@Api(value = "用户API", description = "用户API")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "新增用户")
    @PostMapping
    public RespData<Long> createUser(@RequestBody @Validated UserCreateParam userCreateParam) {
        Long userId = userService.createUser(userCreateParam);
        return RespData.success(userId);
    }

    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户ID", required = true)})
    @GetMapping("/{id}")
    public RespData<UserVO> getUser(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user == null) {
            throw new BizException(ResponseCodeEnum.USER_NOT_FOUND);
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return RespData.success(userVO);
    }

    @ApiOperation(value = "修改用户")
    @PutMapping
    public RespData updateUser(@RequestBody @Validated UserUpdateParam userUpdateParam) {
        userService.updateUser(userUpdateParam);
        return RespData.success();
    }

    @ApiOperation(value = "删除用户信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户ID", required = true)})
    @DeleteMapping("/{id}")
    public RespData deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return RespData.success();
    }

    @ApiOperation(value = "启用、禁用用户")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户ID", required = true)})
    @PutMapping("/forbid/{id}")
    public RespData forbidUser(@PathVariable Long id, @RequestParam Integer status) {
        userService.forbidUser(id, status);
        return RespData.success();
    }

    @ApiOperation(value = "重置用户密码")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户ID", required = true)})
    @PutMapping("/reset-password/{id}")
    public RespData resetPassword(@PathVariable Long id) {
        userService.resetPassword(id);
        return RespData.success();
    }

    @ApiOperation(value = "修改用户密码")
    @PutMapping("/change-password")
    public RespData changePassword(@RequestBody @Validated PasswordChangeParam passwordChangeParam) {
        userService.changePassword(passwordChangeParam);
        return RespData.success();
    }

    @ApiOperation(value = "分页查询用户列表")
    @PostMapping("/page-list")
    public RespData<PageVO<UserVO>> pageList(@RequestBody UserQueryParam userQueryParam) {
        IPage<User> page = userService.pageList(userQueryParam);
        List<UserVO> userVOList = TLBeanUtils.copyList(page.getRecords(), UserVO.class);
        PageVO<UserVO> pageVO = new PageVO<>(userVOList, page.getTotal(), page.getPages(), page.getCurrent(), page.getSize());
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "查询全部用户")
    @GetMapping("/list-all")
    public RespData<List<UserVO>> listAll() {
        List<User> userList = userService.list();
        List<UserVO> userVOList = TLBeanUtils.copyList(userList, UserVO.class);
        return RespData.success(userVOList);
    }
}
